/*
 * @Description: IndexDB数据库 - 公共函数
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-09-28 15:08:41
 * @LastEditTime: 2021-09-28 16:29:29
 * @LastEditors: 0001100986/LiuShuai
 */

// const DATABASE_NAME = "ione_db";
// const VERSION = 1;
// const TABLE = "ione_table";

// 获取数据库对象 Connection DB
export function clientDB (dbName, version, tableName) {
  return new Promise(function(resolve, reject) {
    let request = window.indexedDB.open(dbName, version);
    request.onsuccess = function(e) {
      let db = e.target.result;
      resolve(db);
    };
    request.onupgradeneeded = function(e) {
      // 初始化数据库
      let db = e.target.result;
      if (!db.objectStoreNames.contains(tableName)) {
        let objectStore = db.createObjectStore(tableName, { autoIncrement: true });
        // 创建唯一索引，unique：唯一
        objectStore.createIndex('name', 'name', { unique: true });
      }
    };
  });
}

// 插入一条记录 INSERT INTO DB Table Data
export function insertInto (db, tableName, data) {
  return new Promise(function(resolve, reject) {
    let request = db.transaction(tableName, 'readwrite').objectStore(tableName).add(data);
    request.onsuccess = function() {
      resolve({ status: '0' });
    };
    request.onerror = function(e) {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject({ status: '1', msg: e.target.error });
    };
  });
}

// 查询表 SELECT DB Table
export function selectTable (db, tableName) {
  return new Promise(function(resolve, reject) {
    let objectStore = db.transaction(tableName).objectStore(tableName);
    let records = [];
    objectStore.openCursor().onsuccess = function(e) {
      let cursor = e.target.result;
      if (cursor) {
        records.push(cursor.value);
        cursor.continue();
      } else {
        resolve({ status: '0', data: records });
      }
    };
    objectStore.openCursor().onerror = function(e) {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject({ status: '1', msg: e.target.error });
    };
  });
}

// 查询某一条记录 SELECT DB Table Where Key
export function selectKey (db, tableName, key) {
  return new Promise(function(resolve, reject) {
    let request = db.transaction(tableName).objectStore(tableName).get(key);
    request.onsuccess = function(e) {
      resolve({ status: '0', data: e.target.result });
    };
    request.onerror = function(e) {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject({ status: '1', msg: e.target.error });
    };
  });
}

// 根据索引查询 SELECT DB Table Where [index]
export function selectIndex (db, tableName, _index, identify) {
  return new Promise(function(resolve, reject) {
    let request = db.transaction(tableName, 'readwrite').objectStore(tableName).index(_index).get(identify);
    request.onsuccess = function(e) {
      resolve({ status: '0', data: e.target.result });
    };
    request.onerror = function(e) {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject({ status: '1', msg: e.target.error });
    };
  });
}

// 更新某一条记录 UPDATE DB Table Where Key
export function updateKey (db, tableName, data, key) {
  return new Promise(function(resolve, reject) {
    let request = db.transaction(tableName, 'readwrite').objectStore(tableName).put(data, key);
    request.onsuccess = function() {
      resolve({ status: '0' });
    };
    request.onerror = function(e) {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject({ status: '1', msg: e.target.error });
    };
  });
}

// 删除某一条记录 DELETE DB Table Where key
export function deleteKey (db, tableName, key) {
  return new Promise(function(resolve, reject) {
    let request = db.transaction(tableName, 'readwrite').objectStore(tableName).delete(key);
    request.onsuccess = function() {
      resolve({ status: '0' });
    };
    request.onerror = function(e) {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject({ status: '1', msg: e.target.error });
    };
  });
}